package com.servlet;

import com.dao.Impl.GoodsDaoImpl;
import com.dao.Impl.StaffDaoImpl;
import com.dao.Impl.StorageDaoImpl;
import com.dao.StorageDao;
import com.entity.PageBean;
import com.entity.Staff;
import com.entity.Storage;
import com.util.MyUtil;
import com.util.TransBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jie
 */
@WebServlet(value = "/storageServlet/*")
public class StorageServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String uri = request.getRequestURI();
        String method = uri.substring(uri.lastIndexOf("/") + 1);
        String target = null;
        switch (method){
            case "findStorageId":
                target=findStorageId(request,response);
                break;
            case "addStorage":
                target=addStorage(request,response);
                break;
            case "findPage":
                findPage(request,response);
                break;
            case "findByStorageId":
                findByStorageId(request,response);
                break;
            case "insertStorage":
                target=insertStorage(request,response);
                break;
            case "modifyStatus":
                modifyStatus(request, response);
                break;
            case "modifyMomo":
                modifyMomo(request, response);
                break;
            default:
                break;
        }
        if (target!=null){
            request.getRequestDispatcher(target).forward(request,response);
        }
    }

    private void modifyMomo(HttpServletRequest request, HttpServletResponse response) {
        Storage storage = new Storage();
        TransBean.populate(storage, request.getParameterMap());
        StorageDaoImpl storageDao = new StorageDaoImpl();
        int i = storageDao.modifyMomo(storage);
        if (i != 0) {
            GoodsDaoImpl goodsDao = new GoodsDaoImpl();
            List<Integer> goodsIds = storageDao.findGoodsIdByStorageId(storage.getStorageId());
            if (goodsIds != null) {
                for (Integer goodsId : goodsIds) {
                    goodsDao.modifyNumStatus(goodsId);
                }
            }
        }
        MyUtil.jsonWriter(response, i);
    }

    private void modifyStatus(HttpServletRequest request, HttpServletResponse response) {
        new StorageDaoImpl().updateStatus(Integer.parseInt(request.getParameter("id")), 1);
    }

    private String insertStorage(HttpServletRequest request, HttpServletResponse response) {
        Storage st=new Storage();
        TransBean.populate(st,request.getParameterMap());
        StorageDao dao=new StorageDaoImpl();
        int i = dao.insertStorage(st);
        MyUtil.jsonWriter(response,i);
        return null;
    }

    private String addStorage(HttpServletRequest request, HttpServletResponse response) {
        Storage storage=new Storage();
        TransBean.populate(storage,request.getParameterMap());
        StorageDao dao=new StorageDaoImpl();
        int i = dao.addStorage(storage);
        MyUtil.jsonWriter(response,i);
        return null;
    }

    protected String findStorageId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StorageDao dao=new StorageDaoImpl();
        int storageId = dao.findStorageId();
        MyUtil.jsonWriter(response,storageId);
        return null;
    }

    private void findPage(HttpServletRequest request, HttpServletResponse response) {
        PageBean pageBean = new PageBean();
        TransBean.populate(pageBean, request.getParameterMap());
        new StorageDaoImpl().showPage(pageBean);
        MyUtil.jsonWriter(response, pageBean);
    }

    private void findByStorageId(HttpServletRequest request, HttpServletResponse response) {
        MyUtil.jsonWriter(response, new StorageDaoImpl().findStorageByStorageId(Integer.valueOf(request.getParameter("id"))));
    }
}
